import { formatTime } from '../../../utils/util';
import { LogisticsIconMap } from '../config';

Page({
  data: {
    pageLoading: true,
    order: {}, // 后台返回的原始数据
    _order: {}, // 内部使用和提供给 order-card 的数据
    storeDetail: {},
    countDownTime: null,
    addressEditable: false,
    backRefresh: false, // 用于接收其他页面back时的状态
    formatCreateTime: '', //格式化订单创建时间
    logisticsNodes: [],
  },

  onLoad(query) {
    this.orderNum = query.orderNum;
    this.init();
    this.navbar = this.selectComponent('#navbar');
    this.pullDownRefresh = this.selectComponent('#wr-pull-down-refresh');
  },

  onShow() {
    // 当从其他页面返回，并且 backRefresh 被置为 true 时，刷新数据
    if (!this.data.backRefresh) return;
    this.onRefresh();
    this.setData({ backRefresh: false });
  },

  onPageScroll(e) {
    this.pullDownRefresh && this.pullDownRefresh.onPageScroll(e);
  },

  onImgError(e) {
    if (e.detail) {
      console.error('img 加载失败');
    }
  },

  // 页面初始化，会展示pageLoading
  init() {
    this.setData({ pageLoading: true });
    this.getDetail()
  },

  getDetail() {
    wx.cloud.database().collection('book-order').where({
      orderNum: this.orderNum,
    }).get({
      success:res=>{
        this.setData({
          pageLoading: false,
          order: res.data[0],
          formatCreateTime: formatTime(
            parseFloat(`${res.data[0]._createTime}`),
            'YYYY-MM-DD HH:mm',
          ), // 格式化订单创建时间
        });
      }
    })
    
    // return fetchOrderDetail(params).then((res) => {
      
    //   // this.setData({
    //   //   order,
    //   //   _order,
    //   //   formatCreateTime: formatTime(
    //   //     parseFloat(`${order.createTime}`),
    //   //     'YYYY-MM-DD HH:mm',
    //   //   ), // 格式化订单创建时间
    //   //   countDownTime: this.computeCountDownTime(order),
    //   //   isPaid: !!order.paymentVO.paySuccessTime,
    //   //   logisticsNodes: this.flattenNodes(order.trajectoryVos || []),
    //   // });
    // });
  },

  // 展开物流节点
  flattenNodes(nodes) {
    return (nodes || []).reduce((res, node) => {
      return (node.nodes || []).reduce((res1, subNode, index) => {
        res1.push({
          title: index === 0 ? node.title : '', // 子节点中仅第一个显示title
          desc: subNode.status,
          date: formatTime(+subNode.timestamp, 'YYYY-MM-DD HH:mm:ss'),
          icon: index === 0 ? LogisticsIconMap[node.code] || '' : '', // 子节点中仅第一个显示icon
        });
        return res1;
      }, res);
    }, []);
  },

  // 拼接省市区
  composeAddress(order) {
    return [
      //order.logisticsVO.receiverProvince,
      order.logisticsVO.receiverCity,
      order.logisticsVO.receiverCountry,
      order.logisticsVO.receiverArea,
      order.logisticsVO.receiverAddress,
    ]
      .filter((s) => !!s)
      .join(' ');
  },
  onGoodsCardTap(e) {
    const { index } = e.currentTarget.dataset;
    const goods = this.data.order.orderItemVOs[index];
    wx.navigateTo({ url: `/pages/goods/details/index?spuId=${goods.spuId}` });
  },
  onOrderNumCopy() {
    wx.setClipboardData({
      data: this.data.order.orderNum,
    });
  },
});
